<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="button" value="发送验证码" id="sendCode">
    <script>
        var timer;
        var obj={
            s:20
        };
        
        var btn=document.getElementById("sendCode");
        function show(objs){//形参
           
            if(objs.s<0){
                clearInterval(timer);
                this.disabled=false;//启用按钮
                this.value=`发送验证码`;
                objs.s=20;
            }else{
                this.disabled=true;//禁用按钮
                this.value=`${objs.s}秒后重发`;
                objs.s--;
            }
        }
        btn.onclick=function(){

            //bind目的:是将show函数中的this绑定到btn上
            timer=setInterval(show.bind(btn,obj),200);
        };

        
    </script>
</body>
</html>